package com.frxuexi.myroom.view

import androidx.compose.foundation.background
import androidx.compose.foundation.clickable
import androidx.compose.foundation.layout.Arrangement
import androidx.compose.foundation.layout.Column
import androidx.compose.foundation.layout.Row
import androidx.compose.foundation.layout.fillMaxSize
import androidx.compose.foundation.layout.fillMaxWidth
import androidx.compose.foundation.layout.padding
import androidx.compose.foundation.lazy.LazyColumn
import androidx.compose.foundation.lazy.items
import androidx.compose.foundation.shape.RoundedCornerShape
import androidx.compose.material.icons.Icons
import androidx.compose.material.icons.filled.ArrowBack
import androidx.compose.material3.Icon
import androidx.compose.material3.Text
import androidx.compose.runtime.Composable
import androidx.compose.runtime.LaunchedEffect
import androidx.compose.runtime.remember
import androidx.compose.runtime.snapshots.SnapshotStateList
import androidx.compose.ui.Modifier
import androidx.compose.ui.graphics.Color
import androidx.compose.ui.res.colorResource
import androidx.compose.ui.unit.dp
import androidx.hilt.navigation.compose.hiltViewModel
import androidx.navigation.NavHostController
import com.frxuexi.lib_base.R
import com.frxuexi.lib_base.RoutePath
import com.frxuexi.lib_base.UiState
import com.frxuexi.lib_base.room.Leave
import com.frxuexi.myroom.intent.LeaveIntent
import com.frxuexi.myroom.viewmodel.LeaveViewModel
import com.google.gson.Gson
import kotlinx.coroutines.launch

/**
 * 未审核请假列表页面
 */
@Composable
fun RoomApproveLeave(navHostController: NavHostController,vm: LeaveViewModel = hiltViewModel()) {
    var list = remember {
        SnapshotStateList<Leave>()
    }

    LaunchedEffect(key1 = "", block = {
        launch {
            vm.state.collect{
                when(it){
                    is UiState.onSuccess<*> ->{
                        val entities = it.data as Collection<Leave>
                        list.clear()
                        list.addAll(entities)
                    }

                    else -> {}
                }
            }
        }

        vm.sendIntent(LeaveIntent.selectNoApprove("未审核"))
    })

    Column(
        Modifier
            .fillMaxSize()
            .padding(20.dp), verticalArrangement = Arrangement.spacedBy(10.dp)) {
        Row (
            Modifier
                .fillMaxWidth()
            , horizontalArrangement = Arrangement.SpaceBetween){
            Icon(imageVector = Icons.Default.ArrowBack, contentDescription = "", Modifier.clickable {
                navHostController.popBackStack()
            })
            Text(text = "未审核记录")
            Text(text = "" )
        }
        Row(
            Modifier
                .fillMaxWidth()
                .background(colorResource(id = R.color.lightblue))
                .padding(vertical = 15.dp)
            , horizontalArrangement = Arrangement.SpaceAround) {
            Text(text = "类型")
            Text(text = "开始时间")
            Text(text = "结束时间")
            Text(text = "审批状态")
        }
        LazyColumn(verticalArrangement = Arrangement.spacedBy(10.dp), content = {
            items(list){
                ApproveLeaveItem(entity = it, navHostController = navHostController)
            }
        })
    }
}

@Composable
fun ApproveLeaveItem(entity: Leave?,navHostController: NavHostController){
    Row(
        Modifier
            .fillMaxWidth()
            .background(Color.LightGray, RoundedCornerShape(8.dp))
            .padding(vertical = 15.dp)
        , horizontalArrangement = Arrangement.SpaceAround) {
        Text(text = entity?.type!!)
        Text(text = entity.startTime?.split(" ")!![0])
        Text(text = entity.endTime?.split(" ")!![0])

        Text(text = "审核" ,
            Modifier.background(Color.Gray, RoundedCornerShape(5.dp)).padding(10.dp,3.dp).clickable {
                val json = Gson().toJson(entity)
                navHostController.navigate(RoutePath.ROOMCOM+"/$json")
            })
    }
}
